package domein;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.MulticastSocket;

/**
 * 
 * @author rensbonnez
 * Class that take care of the shared socket. In this way host can
 * send broadcasts very easily.
 * 
 */

public class SharedSocket 
{
	private MulticastSocket socket;
	private static SharedSocket sharedSocket;
	private final int defaultBroadcastPort = 8501;
	
	private SharedSocket()
	{
		try 
		{
			socket = new MulticastSocket(defaultBroadcastPort);
		} 
		catch (Exception e) 
		{
			System.exit(1);
		}
	}
	
	public static MulticastSocket getInstance()
	{
		if(sharedSocket == null)
			sharedSocket = new SharedSocket();
		return sharedSocket.socket;
	}
	
	public static void sendPacket(DatagramPacket packet) throws IOException
	{
		getInstance().send(packet);
	}

	public MulticastSocket getSocket() {
		return socket;
	}

	public void setSocket(MulticastSocket socket) {
		this.socket = socket;
	}

	public static void setSharedSocket(SharedSocket sharedSocket) {
		SharedSocket.sharedSocket = sharedSocket;
	}

	public int getDefaultBroadcastPort() {
		return defaultBroadcastPort;
	}
}
